﻿//********************************************************************************//
//创建日期：<创建日期，2016-12-15>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：应急预案附件列表
//********************************************************************************//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Platform.Http;
using Web.Class;
using Platform.Common;
using DevExpress.Web;
using System.IO;
using System.Text;

namespace Web.Pages.YongShui
{
    public partial class YingJiYAFJLB : BasePage
    {
        #region 属性

        /// <summary>
        /// 保存结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        private string UploadDirectory = "/UPLOAD/YJ/";  //图片上传文件夹路径

        #endregion

        #region 变量

        YS_YJ_FUJIANProcess _YS_YJ_FUJIANBLL = new YS_YJ_FUJIANProcess();

        /// <summary>
        /// 应急预案附件对象
        /// </summary>
        YS_YJ_FUJIAN _YS_YJ_FUJIAN = new YS_YJ_FUJIAN();

        /// <summary>
        /// 操作日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
                
            }            
            LoadData();
        }

        /// <summary>
        /// 响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            CallbackResult cr = new CallbackResult(e.Parameter);
            int id = Convert.ToInt32(this.hdCollection.Get("ID"));
            string savePath = Server.MapPath(UploadDirectory);
            string fullPath = null;
            if (e.Parameter == "del")
            {
                _YS_YJ_FUJIAN = _YS_YJ_FUJIANBLL.SelectYS_YJ_FUJIAN(id);
                //执行逻辑删除            
                _YS_YJ_FUJIAN.S_CAOZUOR = LoginInfo.CurrentUser.Account;
                _YS_YJ_FUJIAN.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
                _YS_YJ_FUJIAN.I_JLZT = -1;
                _SYS_LOGBLL.Log<YS_YJ_FUJIAN>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "应急预案", OperateState.Delete, _YS_YJ_FUJIAN, null);
                if (_YS_YJ_FUJIANBLL.InsertOrUpdate(_YS_YJ_FUJIAN))
                {
                                         
                    //删除文件
                    fullPath = savePath + _YS_YJ_FUJIAN.S_FILESERVERNAME;
                    if (System.IO.File.Exists(fullPath))
                    {
                        System.IO.File.Delete(fullPath);
                    }
                    cr.IsSuccess = true;
                    cr.IsAlertMsg = true;
                    cr.Msg = "操作成功！";
                    e.Result = cr.ToString();
                    return;
                }
            }
            //else if (e.Parameter == "download")
            //{
            //    string filePath = "aaa";
            //    //string filePath = Server.MapPath(this.UploadDirectory + this._YS_YJ_FUJIAN.S_FILESERVERNAME);
            //    FileStream fs = new FileStream(filePath, FileMode.Open);
            //    byte[] bytes = new byte[(int)fs.Length];
            //    fs.Read(bytes, 0, bytes.Length);
            //    fs.Close();
            //    Response.ContentType = "application/octet-stream";//通知浏览器下载文件而不是打开
            //    Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filePath, System.Text.Encoding.UTF8));
            //    Response.BinaryWrite(bytes);
            //    Response.Flush();
            //    Response.End();
            //    cr.IsSuccess = true;
            //    cr.IsAlertMsg = false;
            //    cr.Msg = "";
            //    e.Result = cr.ToString();
            //    return;
            //}
        }

        /// <summary>
        /// 前台验证后实际执行后台操作
        /// </summary>
        protected void btnActPerformExport_Click(object sender, EventArgs e)
        {
            //string path = this.hdCollection.Get("filepath").ToString();
            //this.Context.Response.WriteFile(path);
        }

        protected void gvList_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadData();
        }

        #endregion

        #region 方法
        
        
        private void LoadData()
        {
            if (!string.IsNullOrEmpty(Request["yjid"]))
            {
                this.dtSource = this._YS_YJ_FUJIANBLL.SelectYS_YJ_FUJIANsDynamic("I_JLZT=0 AND I_YINGJIYAID=" + Request["yjid"], null);
            }
            else
            {
                this.dtSource = null;
            }
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
        }

        #endregion

        protected void btnDownLoad_Command(object sender, CommandEventArgs e)
        {
            this._YS_YJ_FUJIAN = this._YS_YJ_FUJIANBLL.SelectYS_YJ_FUJIAN(Convert.ToInt32(e.CommandArgument));
            string filePath = Server.MapPath(this.UploadDirectory + this._YS_YJ_FUJIAN.S_FILESERVERNAME);
            FileStream fs = new FileStream(filePath, FileMode.Open);
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            Response.ContentType = "application/octet-stream";//通知浏览器下载文件而不是打开
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(this._YS_YJ_FUJIAN.S_FILENAME, System.Text.Encoding.UTF8));
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }
       
    }

}